package lambda.jdkfunctioninterface.消费型;

import java.util.function.Consumer;
import org.testng.annotations.Test;

/**
 * Consumer<T> 表示要在一个输入参数上执行的操作，泛型指的是要操作的参数的类型，而不是方法的返回类型
 *
 * @author
 */
public class Consumer1 {

    static class Ball {
        private String name;
        private int price;

        public Ball(String name, int price) {
            this.name = name;
            this.price = price;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }
    }

    @Test
    public void t1 () {

        Consumer<String> consumer = System.out::println;

        // 泛型参数表明了接受的参数的类型
        Consumer<Ball> consumer2 = ball -> System.out.println(ball.name);

        consumer.accept(null);

        consumer2.accept(new Ball("Ten", 99));
    }

    @Test
    public void t2() {
        operateString("hello", System.out::println, s -> System.out.println(s.toUpperCase()));
    }

    /**
     * 泛型指的是要操作的参数的类型，而不是方法的返回类型
     * System.out::println的返回类型时void，但是操作数的类型时String，所以可以作为参数传入
     */
    public void operateString(String name, Consumer<String> con1, Consumer<String> con2) {
        con1.andThen(con2).accept(name);
    }

}




